package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderDetailVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/order")
@Slf4j
@Tag(name = "订单管理相关接口")
@Validated
public class OrderController {
    @Autowired
    private OrdersService orderService;

    /**
     * 订单搜索
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @Operation(summary = "订单搜索")
    public Result<PageResult<OrderVO>> conditionSearch(@Valid OrdersPageQueryDTO ordersPageQueryDTO) {
        PageResult<OrderVO> orderPageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(orderPageResult, MessageConstant.ORDER_SEARCH_SUCCESS);
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    @Operation(summary = "各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics() {
        OrderStatisticsVO orderOverViewVO = orderService.statistics();
        return Result.success(orderOverViewVO, MessageConstant.ORDER_STATISTICS_SUCCESS);
    }

    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @Operation(summary = "派送订单")
    public Result<String> delivery(@PathVariable Long id) {
        orderService.delivery(id);
        return Result.success(MessageConstant.DELIVERY_SUCCESS);
    }
    /**
     * 接单
     */
    @PutMapping("/confirm")
    @Operation(summary = "接单")
    public Result<String> confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        orderService.confirm(ordersConfirmDTO);
        return Result.success(MessageConstant.CONFIRM_ORDER_SUCCESS);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("details/{id}")
    @Operation(summary = "查询订单详情")
    public Result<OrderDetailVO> getOrderDetail(@PathVariable("id") Long id) {
        log.info("查询订单详情，订单id：{}", id);
        OrderDetailVO orderDetailVO = orderService.getOrderDetail(id);
        return Result.success(orderDetailVO, MessageConstant.GET_ORDER_DETAIL_SUCCESS);
    }

    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping("/cancel")
    @Operation(summary = "取消订单")
    public Result<String> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        orderService.cancel(ordersCancelDTO);
        return Result.success(MessageConstant.CANCEL_ORDER_SUCCESS);
    }

    /**
     * 拒单
     * @param rejectionDTO
     * @return
     * @throws Exception
     */
    @PutMapping("/rejection")
    @Operation(summary = "拒单")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO rejectionDTO) throws Exception {
        orderService.rejection(rejectionDTO);
        return Result.success(MessageConstant.REJECT_ORDER_SUCCESS);
    }

    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @Operation(summary = "完成订单")
    public Result<String> complete(@PathVariable Long id) {
        orderService.complete(id);
        return Result.success(MessageConstant.COMPLETE_ORDER_SUCCESS);
    }



}
